<?php
namespace App\Http\Controllers;

use App\Model\News;
use Carbon\Carbon;
use Symfony\Component\HttpFoundation\Request;

class HomeController extends Controller {

    function index(Request $request) {
        $site = $request->input('site');
        $news = null;
        if (empty($site)) {
            $news = News::paginate(20);
        }
        else {
            $news = News::where('site', $site)->paginate(20);
        }

        // print_r($news);
        // exit;

        foreach ($news as $new) {
            $new->create_at = Carbon::parse($new->create_at, 'Asia/Shanghai')->diffForHumans();
        }

        return view('welcome', ['news' => $news]);
    }

    function link($id) {
        $new = News::findOrFail($id);
        $new->increment('counter');
        return redirect($new->link);
    }
}
